package com.LC.Controller.BKManagement;

import com.LC.Constant.MessageConstant;
import com.LC.Entity.*;
import com.LC.Service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;
    
    @RequestMapping("/BKManagement/CheckGroup/ItemList")
    public Result showAll(){
        List<CheckItem> checkItemList = null;
        try {
            checkItemList = checkGroupService.showAll();
        } catch (Exception e) {
            new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }
    
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/BKManagement/CheckGroup/Page")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.findPage(queryPageBean);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/BKManagement/CheckGroup/New")
    public Result addGroup(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.addCheckGroup(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @RequestMapping("/BKManagement/CheckGroup/CheckGroupById")
    public Result findCheckGroupById(Integer CheckGroupId){
        CheckGroup checkGroupById;
        try {
            checkGroupById = checkGroupService.findCheckGroupById(CheckGroupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupById);
    }

    /**
     * 由检查组id查询其包含的检查项id
     * @return
     */
    @RequestMapping("/BKManagement/CheckGroup/CheckItemIdsByGroupId")
    public List<Integer> IncludeIds(Integer CheckGroupId){
        List<Integer> includeIds = null;
        try {
            includeIds = checkGroupService.IncludeIds(CheckGroupId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return includeIds;
    }
    
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @RequestMapping("/BKManagement/CheckGroup/Update")
    public Result UpdateFormAndIds(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.UpdateFormAndIds(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @RequestMapping("/BKManagement/CheckGroup/De")
    public Result deleteCheckGroupById(int checkGroupId){
        try {
            checkGroupService.deleteCheckGroupById(checkGroupId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

}
